package com.baizhi.cmfz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/util")
@Controller
public class UtilController {
    /**
     * 上传轮播图图片 此处不再使用虚拟路径 直接上传到项目中去
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Map upload(MultipartFile file, HttpServletRequest request) throws IOException {
        String newName= UUID.randomUUID().toString()+ file.getOriginalFilename();
        String realPath = request.getServletContext().getRealPath("/img/" + newName);
        System.out.println(realPath);
        file.transferTo(new File(realPath));

        //响应给layui信息
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("src","/img/" + newName);
        return map;
    }

}
